<?php 
	class FacebookModel extends Model {
	public function __construct(){
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
		private $url = "" ;
		private $width = 80;
		private $height = 80;
		//actionType : like or recommend 
		private $action_type = "like" ;
		private $numpost = 5;
		private $showface = true ;
		private $share = true ;
		private $showheader = true ;
		private $showborder = true ;
		private $showpost = true ;
		private $color_sheme = "light";
		private $stream = true;
		private $layout = "button_count";
		private $layouts = array("standard","box_count","button_count","button","icon_link","icon","link");

		public function getLayout($layouts=array(),$name=""){
			if($layouts!="" && is_array($layouts) && $name!=""){
				foreach ($layouts AS $layout){
					return ($layout === $name)?$layout:$this->layout ;
				}
			}
		}
		
		//set all setting 
		public function setOptionValue($setting=array()){
			if($setting!=""&is_array($setting)){
				foreach ($setting AS $option=>$value){
					switch ( $option ){
						case "url" : $this->url = $value ; break;
						case "width" : $this->width = $value . "px" ;break;
						case "height" : $this->height = $value ."px";break;
						case "layout" : $this->layout = $this->getLayout($this->layouts,$value);break;
						case "showface" : $this->showface = $value==1?"true":"false";break;
						case "share" : $this->share = $value==1?"true":"false";break;
						case "stream" : $this->stream = $value==1?"true":"false";break;
						case "action" : $this->action = $value;break;
						case "colorsheme" : $this->color_sheme = $value ;break;
						case "showheader": $this->showheader = $value==1?"true":"false";break;
						case "showborder" : $this->showborder = $value==1?"true":"false";break;
						case "numpost": $this->postcount = $value ; break; 
						case "showpost" : $this->showpost = $value==1?"true":"false";break;
						default : $this->layout = "standard";break;
					}
				}
			}
		}
		
		public function jsScript(){
			?>
			<div id="fb-root"></div>
			<script>(function(d, s, id) {
				  var js, fjs = d.getElementsByTagName(s)[0];
				  if (d.getElementById(id)) return;
				  js = d.createElement(s); js.id = id;
				  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
				  fjs.parentNode.insertBefore(js, fjs);
				}(document, 'script', 'facebook-jssdk'));</script>
			<?php 
		}
		
		public function LikeButton($options=array()){
			$this->setOptionValue($options);?>
			<div class="fb-like" data-href="https://www.facebook.com/<?php echo $this->url?>" data-width="<?php echo $this->width;?>" data-layout="<?php echo $this->layout?>" data-show-faces="<?php echo $this->showface?>" data-share="<?php echo $this->share?>" data-height="<?php echo $this->height?>"></div>
			<?php
		}
		public function LikeButtonAsURL ($options=array()){
			self::setOptionValue($options);
			$url = "https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2F";
			$url.= $this->url."&width=".$this->width."&height=".$this->height."&layout=".$this->layout;
			$url.="&action=".$this->action_type."&show_faces=".$this->show_face."&share=".$this->share;
			return $url;
		}
		
		public function LikeBox($options=array()){
			$this->setOptionValue($options);?>
			<div class="fb-like-box" data-href="http://www.facebook.com/<?php echo $this->url ?>" data-colorscheme="<?php echo $this->color_sheme?>" data-show-faces="<?php echo $this->showface?>" data-header="<?php echo $this->showheader?>" data-stream="<?php echo $this->stream?>" data-show-border="<?php echo $this->showborder?>"></div>
			<?php 
		}
		public function LikeBoxAsURL($options=array()){
			$this->setOptionValue($options);
			$url ="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2F";
			$url.=$this->url."&width=".$this->url."&height=".$this->height."&colorscheme=".$this->color_sheme;
			$url.="&show_faces=".$this->show_face."&header=".$this->showheader."&stream=".$this->stream."&show_border=".$this->showborder;
			return $url;
		}
		public function Share($options=array()){
			$this->setOptionValue($options);?>
			<div class="fb-share-button" data-href="<?php echo $this->url?>" data-type="<?php echo $this->layout?>"></div>
			<?php 
		}
		
		public function Comment($setting=array()){
			$this->setOptionValue($setting);?>
			<div class="fb-comments" data-href="<?php echo $this->url?>" data-numposts="<?php echo $this->numpost?>" data-colorscheme="<?php echo $this->color_sheme?>"></div>
			<?php 
		}
		
		public function Send($options=array()){
			$this->setOptionValue($options);?>
			<div class="fb-send" data-href="<?php echo $this->url?>" data-colorscheme="<?php echo $this->color_sheme?>"></div>
			<?php
		}
	}